<?php
/* --------------------------------------------------------------
   google_shopping.php 2012-05-09 gm
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2012 Gambio GmbH
   Released under the GNU General Public License
   --------------------------------------------------------------
*/
?><?php

class Google_Shopping
{
	var $v_partnerlink;
	var $v_export_type;
	var $v_module_name;
	var $v_module_homepage;
	var $v_module_format;
	var $v_module_export_filename;
	var $v_filename;
	var $v_keyname;
	var $v_linkname;
	var $v_delimiter;
	var	$v_enclosure;

	var $v_field_filename;
	var $v_field_customers_groups;
	var $v_field_currency;
	var $v_field_shipping_costs;
	var $v_field_shipping_costs_free;
	var $v_field_attributes;
	var $v_field_campaign;
	var $v_field_export;

	var $v_category_file_path;
	var	$v_shipping_availability_array = array();
	var	$v_availability_array = array();

	function Google_Shopping()
	{
		$this->save_config();
		$this->load_availabilities();
		$this->formSettings();
	}

	function formSettings()
	{
		$this->v_partnerlink='';
		$this->v_export_type='comparison';
		$this->v_module_name='Google Shopping';
		$this->v_module_homepage='www.google.de/prdhp';
		$this->v_module_format='txt';
		$this->v_module_export_filename='google_shopping.txt';
		$this->v_filename='google_shopping.php';
		$this->v_keyname='GOOGLE_SHOPPING';
		$this->v_linkname='google_shopping';
		$this->v_delimiter="\t";
		$this->v_enclosure='';

		$this->v_field_filename=true;
		$this->v_field_customers_groups=true;
		$this->v_field_currency=true;
		$this->v_field_shipping_costs=true;
		$this->v_field_shipping_costs_free=true;
		$this->v_field_attributes=true;
		$this->v_field_campaign=true;
		$this->v_field_export=true;

		$this->v_category_file_path = 'http://www.google.com/basepages/producttype/taxonomy.de-DE.txt';
	}

	function formAddOn()
	{
		$t_addOns = '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>Google Kategorie URL</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_input_field('categorie_file_path', gm_get_conf('GM_GOOGLE_SHOPPING_CATEGORY_FILE_PATH', 'ASSOC', true), 'style="width: 450px;"')
				   .'<br />Geben Sie hier den Pfad zur Taxonomy Datei f&uuml;r die Google Produkt-Kategorien an.';

		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>fehlende MPN generieren</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_radio_field('auto_mpn', '0', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_MPN', 'ASSOC', true)) ? false : true).'nein'
				   .'<br />'.xtc_draw_radio_field('auto_mpn', '1', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_MPN', 'ASSOC', true)) ? true : false).'ja'
					.'<br />Sollen fehlende MPN automatisch aus der Artikelnummer oder, falls nicht vorhanden, per Zufall generiert und gespeichert werden?<br /><br />'
					.'<input type="checkbox" name="mpn_attribute_model" value="1"' . ((gm_get_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL') == '1') ? ' checked="checked"' : '') . ' /> Attributartikel-MPN nicht aus Artikelnummer und Attribut-Artikelnummer, sondern nur aus Attribut-Artikelnummer generieren.';

		$t_addOns .= '<tr style="vertical-align:top; background-color:#d6e6f3;">
				   <td class="dataTableContent_gm"><strong>fehlende Marken generieren</strong></td>
				   <td class="dataTableContent_gm">'.xtc_draw_radio_field('auto_brand', '0', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_BRAND', 'ASSOC', true)) ? false : true).'nein'
				   .'<br />'.xtc_draw_radio_field('auto_brand', '1', ((int)gm_get_conf('GM_GOOGLE_SHOPPING_AUTO_BRAND', 'ASSOC', true)) ? true : false).'ja'
					.'<br />Sollen fehlende Marken aus dem Herstellernamen generiert und gespeichert werden?';

		// return addon setup
		return($t_addOns);
	}

	function load_availabilities()
	{
		$t_availability_sql = "SELECT
									s.shipping_status_id,
									a.google_export_availability_id,
									a.availability
								FROM
									shipping_status_to_google_availability s,
									google_export_availability a
								WHERE s.google_export_availability_id = a.google_export_availability_id";
		$t_availability_result = xtc_db_query($t_availability_sql);
		while($t_availability_result_array = xtc_db_fetch_array($t_availability_result))
		{
			$this->v_shipping_availability_array[$t_availability_result_array['shipping_status_id']] = array('ID' => $t_availability_result_array['google_export_availability_id'],
																									'NAME' => $t_availability_result_array['availability']);
		}

		$t_availability_sql = "SELECT
									google_export_availability_id,
									availability
								FROM google_export_availability";
		$t_availability_result = xtc_db_query($t_availability_sql);
		while($t_availability_result_array = xtc_db_fetch_array($t_availability_result))
		{
			$this->v_availability_array[$t_availability_result_array['google_export_availability_id']] = $t_availability_result_array['availability'];
		}
	}

	function save_config()
	{
		if(isset($_POST['auto_mpn']) && isset($_POST['auto_brand']))
		{
			gm_set_conf('GM_GOOGLE_SHOPPING_AUTO_MPN', (int)$_POST['auto_mpn']);
			gm_set_conf('GM_GOOGLE_SHOPPING_AUTO_BRAND', (int)$_POST['auto_brand']);

			if(isset($_POST['mpn_attribute_model']) && $_POST['mpn_attribute_model'] == '1')
			{
				gm_set_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL', 1);
			}
			else
			{
				gm_set_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL', 0);
			}

			if((int)$_POST['auto_mpn'] == 1)
			{
				xtc_db_query("INSERT INTO products_item_codes (products_id)
								SELECT
									p.products_id AS products_id
								FROM
									products AS p LEFT JOIN products_item_codes AS pic USING (products_id)
								WHERE
									google_export_condition IS NULL");

				xtc_db_query("UPDATE
									products AS p,
									products_item_codes AS pic
								SET
									code_mpn = products_model
								WHERE
									pic.products_id = p.products_id AND
									(pic.code_mpn = '' OR pic.code_mpn IS NULL)");

				xtc_db_query("UPDATE
									products_item_codes AS pic
								SET
									code_mpn = UPPER(LEFT(CAST(MD5(pic.products_id) AS CHAR), 5))
								WHERE
									pic.code_mpn = '' OR
									pic.code_mpn IS NULL");

			}

			if((int)$_POST['auto_brand'] == 1)
			{
				xtc_db_query("INSERT INTO products_item_codes (products_id)
								SELECT
									p.products_id AS products_id
								FROM
									products AS p LEFT JOIN products_item_codes AS pic USING (products_id)
								WHERE
									google_export_condition IS NULL");

				xtc_db_query("UPDATE
									products AS p,
									products_item_codes AS pic,
									manufacturers AS m
								SET
									pic.brand_name = m.manufacturers_name
								WHERE
									p.products_id = pic.products_id AND
									p.manufacturers_id = m.manufacturers_id AND
									(pic.brand_name = '' OR pic.brand_name IS NULL)");
			}

			return true;
		}

		return false;
	}

	function formatResults($p_products_array)
	{
		$c_products_id = (int)$p_products_array['products_id'];
		if(isset($p_products_array['products_id_copy']))
		{
			$c_products_id = (int)$p_products_array['products_id_copy'];
		}
		
		$t_google_categories_array = array();

		$t_google_sql = "SELECT google_category
							FROM products_google_categories
							WHERE products_id = '" . $c_products_id . "'";
		$t_google_result = xtc_db_query($t_google_sql);
		while($t_google_result_array = xtc_db_fetch_array($t_google_result))
		{
			$t_google_categories_array[] = $t_google_result_array['google_category'];
		}

		$p_products_array['google_category'] = '';
		if(!empty($t_google_categories_array))
		{
			$p_products_array['google_category'] = '"' . implode('","', $t_google_categories_array) . '"';
		}

		$p_products_array['products_price'] = number_format((double)$p_products_array['products_price'], 2, ',', '');

		$p_products_array['products_shipping_costs'] = 'DE:::' . number_format((double)$p_products_array['products_shipping_costs'], 2, '.', '');

		if(trim($_POST['shipping_costs']) == '')
		{
			$p_products_array['products_shipping_costs'] = '';
		}
		
		$p_products_array['products_description'] = trim(no_html($p_products_array['products_description']));
		if(!empty($p_products_array['products_description']) && strlen($p_products_array['products_description']) > 1500)
		{
			$desc = $p_products_array['products_description'];
			$length = strpos($desc, " ", 1500);
			if($length !== false)
			{
				$p_products_array['products_description'] = substr($p_products_array['products_description'], 0, $length) . '[...]';
			}
		}

		$t_images_array = array();
		for($i = 2; $i <= (int)MO_PICS+1; $i++)
		{
			if(isset($p_products_array['products_image_' . $i]) && !empty($p_products_array['products_image_' . $i]))
			{
				$t_images_array[] = $p_products_array['products_image_' . $i];
			}
		}
		$p_products_array['products_images'] = implode(',', $t_images_array);

		$p_products_array['google_export_gtin'] = '';
		if(!empty($p_products_array['products_ean']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['products_ean']);
		}
		elseif(!empty($p_products_array['code_isbn']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['code_isbn']);
		}
		elseif(!empty($p_products_array['code_upc']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['code_upc']);
		}
		elseif(!empty($p_products_array['code_jan']))
		{
			$p_products_array['google_export_gtin'] = trim($p_products_array['code_jan']);
		}

		if(empty($p_products_array['google_export_condition']))
		{
			$p_products_array['google_export_condition'] = 'neu';
		}

		if(isset($p_products_array['products_attributes_id']) && isset($p_products_array['products_id_copy']))
		{
			$p_products_array['products_id'] = $p_products_array['products_id_copy'] . 'a' . $p_products_array['products_attributes_id'];
		}

		if(empty($p_products_array['google_export_availability_id']) 
				&& !empty($p_products_array['shipping_status_id'])
				&& isset($this->v_shipping_availability_array[(int)$p_products_array['shipping_status_id']]))
		{
			$p_products_array['google_export_availability_id'] = $this->v_shipping_availability_array[(int)$p_products_array['shipping_status_id']]['ID'];
			$p_products_array['google_export_availability'] = $this->v_shipping_availability_array[(int)$p_products_array['shipping_status_id']]['NAME'];
		}
		elseif(!empty($p_products_array['google_export_availability_id']) && isset($this->v_availability_array[(int)$p_products_array['google_export_availability_id']]))
		{
			$p_products_array['google_export_availability'] = $this->v_availability_array[(int)$p_products_array['google_export_availability_id']];
		}

		if(!empty($p_products_array['code_mpn']) && isset($p_products_array['products_attributes_id']))
		{
			if(!empty($p_products_array['attributes_model']))
			{
				if(gm_get_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL') == '1')
				{
					$p_products_array['code_mpn'] = $p_products_array['attributes_model'];
				}
				else
				{
					$p_products_array['code_mpn'] .= $p_products_array['attributes_model'];
				}
			}
			else
			{
				$p_products_array['code_mpn'] .= '-' . $p_products_array['products_attributes_id'];
			}
		}
		elseif(isset($p_products_array['products_attributes_id']) && !empty($p_products_array['attributes_model']) && gm_get_conf('GM_GOOGLE_SHOPPING_MPN_ATTRIBUTE_MODEL') == '1')
		{
			$p_products_array['code_mpn'] = $p_products_array['attributes_model'];
		}
		
		$p_products_array['products_name'] = str_replace("\t", ' ', $p_products_array['products_name']);
		$p_products_array['products_description'] = str_replace("\t", ' ', $p_products_array['products_description']);
		$p_products_array['products_weight'] = (double)$p_products_array['products_weight'] . ' kg';

		return($p_products_array);
	}

	function exportScheme()
	{
		$t_csv_fields_array=array();
		$t_csv_fields_array['products_id'] = 'id';
		$t_csv_fields_array['products_name'] = 'titel';
		$t_csv_fields_array['products_description'] = 'beschreibung';
		$t_csv_fields_array['google_category'] = 'google produktkategorie';
		$t_csv_fields_array['products_categories'] = 'produkttyp';
		$t_csv_fields_array['products_link'] = 'link';
		$t_csv_fields_array['products_image_1'] = 'bildlink';
		$t_csv_fields_array['products_images'] = html_entity_decode('zus&auml;tzlicher bildlink');
		$t_csv_fields_array['google_export_condition'] = 'zustand';
		$t_csv_fields_array['google_export_availability'] = html_entity_decode('verf&uuml;gbarkeit');
		$t_csv_fields_array['products_price'] = 'preis';
		$t_csv_fields_array['brand_name'] = 'marke';
		$t_csv_fields_array['google_export_gtin'] = 'gtin';
		$t_csv_fields_array['code_mpn']= 'mpn';
		if(trim($_POST['shipping_costs']) != '')
		{
			$t_csv_fields_array['products_shipping_costs']= 'versand';
		}		
		$t_csv_fields_array['products_weight']= 'transportgewicht';

		return($t_csv_fields_array);
	}
}
?>